//
//  JWRealmManager.m
//  SiqiCalendar
//
//  Created by sunjiawen on 2018/4/2.
//  Copyright © 2018年 sjw. All rights reserved.
//

#import "JWRealmManager.h"
#import "JWAlarmClockModel.h"
#import "JWScheduleModel.h"
#import "JWCalendarModel.h"

@implementation JWRealmManager
#pragma mark - Singleton
/** 单例 */
static JWRealmManager *manager = nil;

+ (instancetype)defaultManager {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[JWRealmManager alloc] init];

    });
    return manager;
}

#pragma mark - Private
// * 生成64位随机字符串作为主键 */
- (NSString *)ret64bitString {
    char data[64];
    for (int x=0; x <64; data[x++] = (char)('A' + (arc4random_uniform(26))));
    return [[NSString alloc] initWithBytes:data length:64 encoding:NSUTF8StringEncoding];
}

#pragma mark - 日历 JWCalendarModel
- (void)insertWithJWCalendarModel:(JWCalendarModel *)calendarModel {
    WEAKSELF
    RLMRealm *realm = [RLMRealm defaultRealm];
    [realm transactionWithBlock:^{
        calendarModel.calendarId = [weakSelf ret64bitString];
        [realm addObject:calendarModel];
    }];
}

- (void)addOrUpdateWithJWCalendarModel:(JWCalendarModel *)calendarModel {
    if (calendarModel == nil) {
        return;
    }
    
    WEAKSELF
    RLMRealm *realm = [RLMRealm defaultRealm];
    [realm transactionWithBlock:^{
        if (calendarModel.calendarId == nil) {
            calendarModel.calendarId = [weakSelf ret64bitString];
        }
        [realm addOrUpdateObject:calendarModel];
    }];
}

- (NSInteger)numberOfCalendars {
    return [JWCalendarModel allObjects].count;
}

- (RLMResults *)queryAllCalendar {
    // 按照 orderIndex 进行升序排列
    return [[JWCalendarModel allObjects] sortedResultsUsingKeyPath:@"orderIndex" ascending:YES];
}

- (JWCalendarModel *)queryCalendarWithCalendarId:(NSString *)calendarId {
    if (calendarId == nil || calendarId.length <= 0) return nil;
    return [JWCalendarModel objectsWhere:@"calendarId = %@",calendarId].firstObject;
}

- (JWCalendarModel *)queryCalendarDefault {
    RLMResults *calendars = [JWCalendarModel objectsWhere:@"isDefault = %@",@YES];
    if (calendars.count == 0) {
        RLMResults *allcalendar = [self queryAllCalendar];
        if (allcalendar.count > 0) {
            JWCalendarModel *calendar = allcalendar.firstObject;
            [calendar updateIsDefault:@YES];
            return calendar;
        }
        return nil;
    }
    return calendars.firstObject;
}

- (void)deleteCalendarWithCalendarId:(NSString *)calendarId {
    if (calendarId.length <= 0) return;
    JWCalendarModel *calendar = [self queryCalendarWithCalendarId:calendarId];
    RLMRealm *realm = RLMRealm.defaultRealm;
    
    [realm transactionWithBlock:^{
        [realm deleteObject:calendar];
    }];
}

- (void)updateAllCalendarOrderIndex:(NSArray *)calendars {
    for (int i = 0; i < calendars.count; i++) {
        JWCalendarModel *calendar = calendars[i];
        [calendar updateOrderIndex:@(i)];
    }
}

#pragma mark - 班名 JWScheduleModel
- (void)insertWithScheduleModel:(JWScheduleModel *)scheduleModel {
    WEAKSELF
    [[RLMRealm defaultRealm] transactionWithBlock:^{
        scheduleModel.scheduleId = [weakSelf ret64bitString];
        [[RLMRealm defaultRealm] addObject:scheduleModel];
    }];
}

- (RLMResults *)queryAllSchedules {
    return [[JWScheduleModel allObjects] sortedResultsUsingKeyPath:@"orderIndex" ascending:YES];
}

- (void)updateAllSchedulesOrderIndex:(NSArray *)schedules {
    for (int i = 0; i < schedules.count; i++) {
        JWScheduleModel *schedule = schedules[i];
        [schedule updateOrderIndex:@(i)];
    }
}

- (NSInteger)numberOfSchedule {
    return [JWScheduleModel allObjects].count;
}
#pragma mark - 闹钟 JWAlarmClockModel
- (void)insertWithAlarmClock:(JWAlarmClockModel *)alarmModel {
    WEAKSELF
    [[RLMRealm defaultRealm] transactionWithBlock:^{
        alarmModel.alarmID = [weakSelf ret64bitString];
        [JWAlarmClockModel createInDefaultRealmWithValue:alarmModel];
    }];
}

- (RLMResults *)queryAllAlarmClock {
    return [JWAlarmClockModel allObjects];
}

- (JWAlarmClockModel *)queryAlarmClockWithAlarmID:(NSString *)alarmID {
    if (alarmID.length <= 0) return nil;
    return [JWAlarmClockModel objectsWhere:@"alarmID = %@",alarmID].firstObject;
}

- (void)deleteAlarmClockWithAlarmID:(NSString *)alarmID {
    if (alarmID.length <= 0) return;
    
    JWAlarmClockModel *alarm = [self queryAlarmClockWithAlarmID:alarmID];
    RLMRealm *realm = RLMRealm.defaultRealm;
    [realm transactionWithBlock:^{
        [realm deleteObject:alarm];
    }];
}

@end
